﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Employees
{
    /// <summary>
    /// Interaction logic for EditEmployees.xaml
    /// </summary>
    public partial class EditEmployees : Window
    {
        public EditEmployees()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var w = new AddAnEmployee();
            w.ShowDialog();
            Search();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (lbEmployees.SelectedItem != null)
            {
                Employee.currEmp = (Employee)lbEmployees.SelectedValue;
                var w = new EditAnEmployee();
                w.ShowDialog();
                Search();
            }
            else
            {
                MessageBox.Show("Please select a customer", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            Employee emp = (Employee)lbEmployees.SelectedValue;
            MessageBoxResult msg =  MessageBox.Show("Are you sure you want to delete this employee's info?", "Error!", MessageBoxButton.YesNo, MessageBoxImage.Information, MessageBoxResult.Yes);
            if (msg == MessageBoxResult.Yes)
            {
                Thread thr = new Thread(new ThreadStart(() =>
                    {
                        try
                        {
                            int? iResult = emp.Delete();
                            if (iResult > 0)
                            {
                                MessageBox.Show("Employee's Info was deleted!", "Error!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("An error has occured", "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    }));
                thr.IsBackground = true;
                thr.Start();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
            {
                try
                {
                    Employee.lstEmp = Employee.Search();
                    lbEmployees.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        try
                        {
                            lbEmployees.ItemsSource = Employee.lstEmp;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }

                    }), null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void tbSearch_KeyUp(object sender, KeyEventArgs e)
        {
            Search();
        }

        private void Search()
        {
            string text = tbSearch.Text;
            if (text == "")
            {
                lbEmployees.ItemsSource = Employee.lstEmp;
            }
            else
            {
                Thread thrSearch = new Thread(new ThreadStart(() =>
                {
                    while (true)
                    {
                        if (Employee.lstEmp == null)
                        {
                            Thread.Sleep(50);
                        }
                        else
                        {
                            List<Employee> lstItems = Employee.SearchText(text);
                            lbEmployees.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                lbEmployees.ItemsSource = lstItems;
                            }), null);
                            break;
                        }
                    }
                }));
                thrSearch.IsBackground = true;
                thrSearch.Start();
            }
        }
    }
}
